﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace odinshrine
{
    class Joueur : Humain
    {
        public bool movingright = false;
        public bool movingleft = false;
        public bool moving = false;
        public bool aimlow = false;
        public bool aimup = false;
        public bool flotte = false;

        public Joueur(int vitesse)
            : base()
        {
            this.Vitesse = vitesse;
            this.sauter = true;
            
        }

        //Fonction qui charge tout au niveau du joueur
        public void Load(ContentManager Content, Joueur player, GestionPhysics physEngine)
        {
            player.Load(Content.Load<Texture2D>("Perso2"), 10, 11);
            player.AjoutAnimation("Run", 1, 8);
            player.AjoutAnimation("Idle", 2, 6);
            player.AjoutAnimation("ShootMidAir", 3, 8);
            player.AjoutAnimation("ShootAir", 4, 9);
            player.AjoutAnimation("Kneeling", 5, 6);
            player.AjoutAnimation("KneelingMidAir", 6, 10);
            player.AjoutAnimation("KneelingAir", 7, 10);
            player.AjoutAnimation("JumpMidAir", 8, 8);
            player.AjoutAnimation("JumpAir", 9, 7);
            player.AjoutAnimation("JumpDown", 10, 8);
            player.AjoutAnimation("Jump", 11, 7);
            player.Position.Y = physEngine.sol_y - player.height - 50;
            player.Position.X = 50;
            player.arme = Armerie.Deagle;
            ballesListe.Init();
            this.angle = 0;
        }

        //Gere l'animation du joueur
        public void Animation(GestionPhysics physEngine)
        {
            moving = movingright != movingleft;
            if (!moving)
            {
                movingright = false;
                movingleft = false;
            }


            if (moving)
            {
                if (movingleft)
                {
                    base.SpriteEffect = SpriteEffects.FlipHorizontally;
                    swal = -1;
                }
                else
                {
                    base.SpriteEffect = SpriteEffects.None;
                    swal = 1;
                }



                if (aimlow)
                {
                    if (aimup)
                    {
                        base.ChangeAnime("KneelingMidAir", true);
                        finalAngle = (float)Math.PI / 2; 
                    }


                }
                else if (aimup && !sauter)
                {
                    base.ChangeAnime("ShootMidAir", true);
                    finalAngle = (float)Math.PI / 4;
                }
            }



            if (sauter)
            {
                if (aimlow)
                {
                    base.ChangeAnime("JumpDown", true);
                    finalAngle = (float) -Math.PI / 2;
                }
                else if (aimup)
                {
                    if (moving)
                    {
                        base.ChangeAnime("JumpMidAir", true);
                        finalAngle = (float)Math.PI / 4;

                    }
                    else
                    {
                        base.ChangeAnime("JumpAir", true);
                      finalAngle = (float) Math.PI /2;
                    }
                }
                else
                {
                    base.ChangeAnime("Jump", true);
                    finalAngle = 0;
                }
            }

            else
            {
                if (moving && !aimup)
                {
                    base.ChangeAnime("Run", true);
                    finalAngle = 0;
                }
                else if (aimlow)
                {
                    kneelingplus = 5f;
                    {
                        if (aimup)
                        {
                            base.ChangeAnime("KneelingAir", true);
                            finalAngle = (float)Math.PI / 2;
                        }
                        else
                        {
                            base.ChangeAnime("Kneeling", true);
                            finalAngle = 0;
                        }
                    }
                }


                else
                {
                    kneelingplus = 0f;
                    if (aimup && !moving)
                    {
                        base.ChangeAnime("ShootAir", true);
                        finalAngle = (float)Math.PI / 2;
                    }
                    else if (!moving)
                    {
                        base.ChangeAnime("Idle", true);
                        finalAngle = 0;
                    }
                }
            }

            moving = false;
            movingright = false;
            movingleft = false;
            aimlow = false;
            aimup = false;
        }

        //Gere les deplacements du joueur
        public void Deplacements(KeyboardState clavier, Joueur player, GestionPhysics phys)
        {
            if (clavier.IsKeyDown(Commandes.commandes[0]))
            {
                player.Position.X -= player.Vitesse;
            }
            if (clavier.IsKeyDown(Commandes.commandes[1]))
            {
                player.Position.X += player.Vitesse;
            }
        }   

        //Gere le tir du joueur(collision avec les ennemis etc.)
        public void TirJoueur(KeyboardState clavier, map Map, GestionPhysics physEngine, Sprite balle, GestionEnnemi ennemi, Interface jeu, GameTime gameTime)
        {
            Vector2 posBalle = Vector2.Zero;
            Vector2 posCaisse = Vector2.Zero;

            // Collision Balles / Map

            for (int j = 0; j < ballesListe.tableau.GetLength(0); j++)
            {
                posBalle.X = ballesListe.tableau[j, 0];
                posBalle.Y = ballesListe.tableau[j, 1];

                for( int k = 0; k < Map.structCounter ; k++)
                {
                    if (Collisions.Rectangles(new Rectangle((int)posBalle.X + directionBalles.tableau[j,0] + Map.coefx, (int)posBalle.Y + directionBalles.tableau[j,1] + Map.coefy,balle.Texture.Width,balle.Texture.Height),new Rectangle((int)Map.structlist[k].beg.Pos.X,(int)Map.structlist[k].beg.Pos.Y, Map.structlist[k].counter * 34, 34)))
                    {
                        ballesListe.tableau[j, 0] = -1;
                        ballesListe.tableau[j, 1] = -1;
                        directionBalles.tableau[j, 0] = 0;
                        directionBalles.tableau[j, 1] = 0;
                    }
                }

            }    
              
            MouvementBalles(gameTime);
            // Gestion Tir
            if (clavier.IsKeyDown(Keys.N))
            {

                if ( timer_tirs.Milliseconds >= arme.cadence)
                {
                    lastangle = this.angle;
                    shooted = arme.bpshoot;
                    timer_tirs = TimeSpan.Zero;
                }

                if (timer_tirs.Milliseconds >= (arme.cadence / arme.delay) * (arme.bpshoot - shooted))
                {
                    if (shooted > 0 && ammo != 0 )
                    Tir();
                }

            }


            for (int i = 0; i < 100; i++)
            {
                // Collision ennemi
                try
                {
                    foreach (BadGuys badguys in ennemi.tabEnnemi1)
                    {
                        if (physEngine.collisionRectangleRectangle(new Rectangle(ballesListe.tableau[i, 0], ballesListe.tableau[i, 1], balle.Texture.Width, balle.Texture.Height), new Rectangle((int)badguys.Position.X, (int)badguys.Position.Y, (int)(badguys.scale * badguys.width), (int)(badguys.scale * badguys.height))))
                        {

                            ballesListe.tableau[i, 0] = -1;
                            ballesListe.tableau[i, 1] = -1;

                            if (badguys.Vie - arme.puissance <= 1)
                            {
                                jeu.nbPoints += badguys.Points;
                                ennemi.tabEnnemi1.Remove(badguys);
                            }
                            else
                            {
                                badguys.Vie -= arme.puissance;
                            }
                        }
                    }
                }
                catch { }
            }
            timer_tirs += gameTime.ElapsedGameTime;
        }
    }
}
